package userDao;

import druidPackage.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class GetUserImp implements GetUser {
    @Override
    public User loginUser(User user) {
        QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
        String sql = "select * from admin where name = ? and password = ?";
        User gotUser = null;
        try {
            gotUser = qr.query(sql, new BeanHandler<User>(User.class),user.getName(),user.getPassword());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return gotUser;
    }

    @Override
    public List<Student> getStudents() {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "select * from user";
            List<Student> studentList = null;
            try {
                studentList = qr.query(sql, new BeanListHandler<Student>(Student.class));
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return studentList;
    }

    public int addStudent(Student student){
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "insert into user values(?,?,?,?,?,?)";
            Object[] objects = new Object[]{student.getId(),student.getName(),student.getSex(),student.getDate(),student.getMail(),student.getMessage()};
            int update = qr.update(sql, objects);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
        }
        return 0;
    }

    public int deleteStudent(Student student){
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "delete from user where id = ? and name = ?";
            Object[] objects = new Object[]{student.getId(),student.getName()};
            int update = qr.update(sql, objects);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return 0;
    }

    public int editStudent(Student student){
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "update user set name = ?,sex=?,date=?,mail=?,message=? where id = ?";
            Object[] objects = new Object[]{student.getName(),student.getSex(),student.getDate(),student.getMail(),student.getMessage(),student.getId()};
            int update = qr.update(sql, objects);
            return update;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public Student lookUpStudent(Student student){
        //进行查找前需要遍历数据库中是否存在此对象
        try {
            QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
            String sql = "select * from user";
            List<Student> students = qr.query(sql, new BeanListHandler<Student>(Student.class));
            for (Student studentLooking : students) {
                if (studentLooking.getId() == student.getId()){
                    System.out.println("该学生存在！可以查询");
                    String sql2 = "select * from user where id = ?";
                    Student lookedStudent = qr.query(sql2, new BeanHandler<Student>(Student.class),student.getId());
                    return lookedStudent;
                }

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;

    }



}
